// pages/userSpace/orderCheck/orderCheck.js
const util = require("../../../utils/util")

Page({
  data: {
    arrAddressList: [],
    defaultAddress: null,
    orderInfo: null,
    boolHiddenAddrWindow: true,
    boolHiddenAddrCheck: true,
    currentAddressId: null,
    currentAddress: null
  },

  checkOrder: function() {
    let addressId = this.data.defaultAddress.id;
    util.myRequest({
      url: util.api.cartCheckout,
      data: {addressId},
      success: res =>{
        console.log(res)
      }
    })
  },
  cancelOrder: function() {
    wx.showModal({
      title: '是否取消下单',
      content: '当前订单将被取消',
      success (res) {
        if (res.confirm) {
          wx.navigateBack({
            delta: 1
          })
        } else if (res.cancel) {
        }
      }
    })
    
  },
  confirmOrder: function() {
    console.log("confirmOrder")
    let addressId = this.data.currentAddressId;
    let couponId = this.data.orderInfo.couponList[0].id;
    console.log(addressId, couponId)
    util.myRequest({
      url: util.api.orderSubmit,
      method: "post",
      data: { addressId, couponId },
      success: res =>{
        console.log(res)
        wx.showToast({
          title: "订单提交成功",
          success: () =>{
            let timer = setTimeout(()=>{
              wx.redirectTo({
                url: '/pages/userSpace/orderList/orderList'
              })
              clearTimeout(timer);
              timer = null;
            }, 1600)
          }
        })
      }
    })
  },
  openChangeAddrWindow: function() {
    console.log("openChangeAddrWindow")
    this.setData({
      boolHiddenAddrWindow: false
    })
  },
  checkDefaultAddress: function() {
    this.setData({
      boolHiddenAddrCheck: !!this.data.currentAddress.id
    })
  },

  changeCurrentAddr: function(e) {
    console.log("changeCurrentAddr")
    let id = e.currentTarget.dataset.info.id;
    let index = this.getAddressIndexById(id);
    let addr = this.data.arrAddressList[index];
    this.setData({
      currentAddressId: id,
      currentAddress: addr,
      boolHiddenAddrWindow: true,
      boolHiddenAddrCheck: true
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAddrList()
    .then( this.saveAddrList )
    .then( this.getDefaultAddress )
    .then( this.getOrderInfo )
    .then( this.saveOrderInfo )
    .then( this.checkDefaultAddress )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  getAddrList: function() {
    return new Promise((resolve, reject) =>{
      util.myRequest({
        url: util.api.addressList,
        success: res =>{
          resolve(res.data)
        },
        fail: err =>{
          reject(err)
        }
      })
    })
  },

  saveAddrList: function(res) {
    this.setData({
      arrAddressList: res
    })
  },

  saveOrderInfo: function(res) {
    console.log("saveOrderInfo", res.checkedAddress)
    this.setData({
      orderInfo: res,
      currentAddress: res.checkedAddress,
      currentAddressId:res.checkedAddress.id
    })
  },

  getDefaultAddress: function() {
    let addr = '';
    for (let index = 0; index < this.data.arrAddressList.length; index++) {
      const element = this.data.arrAddressList[index];
      if(!!element.is_default){
        addr = element;
        break;
      }
    }
    this.setData({
      defaultAddress: addr
    })
  },

  getOrderInfo: function() {
    return new Promise((resolve, reject) =>{
      let addressId = this.data.defaultAddress.id;

      util.myRequest({
        url: util.api.cartCheckout,
        data: {addressId},
        success: res =>{
          resolve(res.data)
        },
        fail: err =>{
          reject(err)
        }
      })
    })
  },

  stopBubble: function() {
    // console.log("children")
  },

  quitAddrWindow: function() {
    this.setData({
      boolHiddenAddrWindow: true,
    })
  },

  getAddressIndexById: function(id) {
    let arrAddr = this.data.arrAddressList;
    for (let index = 0; index < arrAddr.length; index++) {
      const element = arrAddr[index];
      if (element.id == id) {
        return index
      }
    }
  }

  // changeDefaultAddr: function() {
  //   console.log('changeDefaultAddr')
  // },
})